import 'package:flutter/material.dart';

import 'package:get/get.dart';
import 'package:im_demo/core/protos/v1/chat.pb.dart';

import '../../../../utils/logger_util.dart';
import '../controllers/register_controller.dart';

class RegisterView extends GetView<RegisterController> {
  const RegisterView({super.key});

  @override
  Widget build(BuildContext context) {
    final controller = Get.put(RegisterController());

    return Scaffold(
      appBar: AppBar(title: const Text('Register'), centerTitle: true),
      body: LayoutBuilder(
        builder: (context, constraints) {
          return SingleChildScrollView(
            child: Container(
              margin: EdgeInsets.fromLTRB(16.0, 8.0, 16.0, 8.0),
              width: double.infinity,
              constraints: BoxConstraints(minHeight: constraints.maxHeight),
              child: Column(
                crossAxisAlignment: CrossAxisAlignment.stretch,
                children: [
                  _buildUrlConfig(),
                  SizedBox(height: 8.0),
                  _buildForm(),
                  SizedBox(height: 8.0),
                  _buildMessageList(),
                ],
              ),
            ),
          );
        },
      ),
    );
  }

  Widget _buildUrlConfig() {
    return Column(
      crossAxisAlignment: CrossAxisAlignment.stretch,
      children: [
        TextField(
          keyboardType: TextInputType.text,
          controller: controller.urlEditController,
          decoration: InputDecoration(
            hintText: '请输入地址',
            // counter: Text(''),
            counterText: '',
            border: OutlineInputBorder(),
            contentPadding: EdgeInsets.fromLTRB(12, 20, 12, 0),
            // contentPadding: EdgeInsets.only(bottom: 0.0),
            suffixIcon: IconButton(
              onPressed: () {
                controller.cleanUrlInputText();
              },
              iconSize: 16.0,
              icon: Icon(Icons.close),
            ),
          ),
          onChanged: (text) {
            controller.updateUrlInputText(text);
          },
        ),
        SizedBox(height: 8.0),
        Row(
          children: [
            ElevatedButton(
              onPressed: () {
                controller.connectWebsocket();
              },
              style: ElevatedButton.styleFrom(
                minimumSize: const Size(64.0, 48.0),
              ),
              child: Text('连接', style: TextStyle(fontSize: 14.0)),
            ),
            SizedBox(width: 8.0),
            ElevatedButton(
              onPressed: () {
                controller.disconnectWebsocket();
              },
              style: ElevatedButton.styleFrom(
                minimumSize: const Size(64.0, 48.0),
              ),
              child: Text('断开', style: TextStyle(fontSize: 14.0)),
            ),
          ],
        ),
      ],
    );
  }

  Widget _buildForm() {
    return Column(
      crossAxisAlignment: CrossAxisAlignment.stretch,
      children: [
        TextField(
          keyboardType: TextInputType.text,
          maxLength: 10,
          controller: controller.userNameEditController,
          decoration: InputDecoration(
            hintText: '请输入用户名',
            // counter: Text(''),
            counterText: '',
            border: OutlineInputBorder(),
            contentPadding: EdgeInsets.fromLTRB(12, 20, 12, 0),
            // contentPadding: EdgeInsets.only(bottom: 0.0),
            suffixIcon: IconButton(
              onPressed: () {
                controller.cleanUserNameInputText();
              },
              iconSize: 16.0,
              icon: Icon(Icons.close),
            ),
          ),
          onChanged: (text) {
            controller.updateUserNameInputText(text);
          },
        ),
        SizedBox(height: 8.0),
        TextField(
          obscureText: true,
          obscuringCharacter: '*',
          enableSuggestions: true,
          autocorrect: true,
          keyboardType: TextInputType.visiblePassword,
          controller: controller.passwordEditController,
          maxLength: 20,
          decoration: InputDecoration(
            hintText: '请输入密码',
            // counter: Text(''),
            counterText: '',
            border: OutlineInputBorder(),
            contentPadding: EdgeInsets.fromLTRB(12, 20, 12, 0),
            // contentPadding: EdgeInsets.only(bottom: 0.0),
            suffixIcon: IconButton(
              onPressed: () {
                controller.cleanPasswordInputText();
              },
              iconSize: 16.0,
              icon: Icon(Icons.close),
            ),
          ),
          onChanged: (text) {
            controller.updatePasswordInputText(text);
          },
        ),
        SizedBox(height: 8.0),
        TextField(
          keyboardType: TextInputType.phone,
          controller: controller.phoneNumberEditController,
          maxLength: 11,
          decoration: InputDecoration(
            hintText: '请输入手机号',
            // counter: Text(''),
            counterText: '',
            border: OutlineInputBorder(),
            contentPadding: EdgeInsets.fromLTRB(12, 20, 12, 0),
            // contentPadding: EdgeInsets.only(bottom: 0.0),
            suffixIcon: IconButton(
              onPressed: () {
                controller.cleanPhoneNumberInputText();
              },
              iconSize: 16.0,
              icon: Icon(Icons.close),
            ),
          ),
          onChanged: (text) {
            controller.updatePhoneNumberInputText(text);
          },
        ),
        SizedBox(height: 8.0),
        TextField(
          keyboardType: TextInputType.text,
          maxLength: 50,
          controller: controller.wechatNumberEditController,
          decoration: InputDecoration(
            hintText: '请输入微信Id',
            // counter: Text(''),
            counterText: '',
            border: OutlineInputBorder(),
            contentPadding: EdgeInsets.fromLTRB(12, 20, 12, 0),
            // contentPadding: EdgeInsets.only(bottom: 0.0),
            suffixIcon: IconButton(
              onPressed: () {
                controller.cleanWechatNumberInputText();
              },
              iconSize: 16.0,
              icon: Icon(Icons.close),
            ),
          ),
          onChanged: (text) {
            controller.updateWechatNumberInputText(text);
          },
        ),
        SizedBox(height: 8.0),
        TextField(
          keyboardType: TextInputType.text,
          maxLength: 100,
          controller: controller.remarksEditController,
          decoration: InputDecoration(
            hintText: '请输入备注',
            // counter: Text(''),
            counterText: '',
            border: OutlineInputBorder(),
            contentPadding: EdgeInsets.fromLTRB(12, 20, 12, 0),
            // contentPadding: EdgeInsets.only(bottom: 0.0),
            suffixIcon: IconButton(
              onPressed: () {
                controller.cleanRemarksInputText();
              },
              iconSize: 16.0,
              icon: Icon(Icons.close),
            ),
          ),
          onChanged: (text) {
            controller.updateRemarksInputText(text);
          },
        ),
        SizedBox(height: 8.0),
        ElevatedButton(
          onPressed: () {
            controller.register();
          },
          child: Text('注册'),
        ),
      ],
    );
  }

  Widget _buildMessageList() {
    return Obx(
      () => ListView.builder(
        shrinkWrap: true,
        itemBuilder: (_, index) => _messageItem(controller.messageList[index]),
        reverse: false,
        itemCount: controller.messageList.length,
      ),
    );
  }

  Widget _messageItem(ChatMessage message) {
    return Container(
      padding: EdgeInsets.fromLTRB(8.0, 8.0, 8.0, 8.0),
      child: Text(message.content),
    );
  }
}
